package com.magicsoft.share.ui.article;

import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.isseiaoki.simplecropview.CropImageView;
import com.isseiaoki.simplecropview.callback.CropCallback;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.magicsoft.share.R;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.base.BasePresenter;
import com.magicsoft.share.utils.FileUtils;
import com.magicsoft.share.utils.OssManager;
import com.magicsoft.share.utils.ToastUtils;

import java.io.File;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CoverActivity extends BaseActivity {


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.cropImageView)
    CropImageView cropImageView;
    @BindView(R.id.btn)
    Button btn;
    private List<LocalMedia> selectList;
    private String coverStr;
    private String coverRiginalStr;
    private Date date;
    private OssManager ossManager;

    @Override
    public BasePresenter initPresenter() {
        return null;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_cover;
    }

    @Override
    public void initData() {

//        coverRiginalStr = getIntent().getStringExtra("coverRiginalStr");
        coverRiginalStr = getIntent().getStringExtra("coverStr");
        date = new Date();
        ossManager = OssManager.getInstance().init(this);
    }

    @Override
    public void initView() {
        ibtnLeft.setVisibility(View.GONE);
        tvLeft.setText("取消");
        tvRight.setText("完成");
        tvTitle.setText("编辑封面");
        Log.d("contentStr", "initView: " + coverStr);
        Glide.with(this).load(coverRiginalStr).into(cropImageView);
        cropImageView.setHandleShowMode(CropImageView.ShowMode.NOT_SHOW);
        cropImageView.setGuideShowMode(CropImageView.ShowMode.NOT_SHOW);
        cropImageView.setCropEnabled(true);
        cropImageView.setCropMode(CropImageView.CropMode.RATIO_16_9);

    }

    @Override
    public void requestData() {

    }


    @OnClick({R.id.tv_left, R.id.tv_right, R.id.btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_right:
                cropImageView.cropAsync(new CropCallback() {
                    @Override
                    public void onSuccess(Bitmap cropped) {

                        File file = FileUtils.saveImage(cropped);
                        Log.d("lyf", "onSuccess: " + file.getPath() + file.getName());
                        ossManager.upload(file.getPath(), new OssManager.uploadListener2() {
                            @Override
                            public void onSuccess(String url) {
                                Log.d("lyf", "onSuccess: " + url);
                                Intent intent = new Intent();
                                intent.putExtra("coverRiginalStr", coverRiginalStr);
                                intent.putExtra("coverStr", url);
                                setResult(RESULT_OK, intent);
                                finish();
                            }

                            @Override
                            public void onFail() {

                                ToastUtils.showToast(CoverActivity.this, "图片出错，请重试");
                            }
                        });

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }
                });

                break;
            case R.id.btn:
                takePhoto();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                     coverRiginalStr = selectList.get(0).getCompressPath();
                    Glide.with(this).load(coverRiginalStr).into(cropImageView);

                    break;
            }
        }
    }

    private void takePhoto() {
        // 进入相册 以下是例子：用不到的api可以不写
        PictureSelector.create(CoverActivity.this)
                .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()
                .theme(R.style.picture_style)//主题样式(不设置为默认样式) 也可参考demo values/styles下 例如：R.style.picture.white.style
                .maxSelectNum(1)// 最大图片选择数量 int
                .minSelectNum(1)// 最小选择数量 int
                .imageSpanCount(4)// 每行显示个数 int
                .selectionMode(PictureConfig.SINGLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                .previewImage(true)// 是否可预览图片 true or false
                .isCamera(false)// 是否显示拍照按钮 true or false
                .sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
                .compress(true)// 是否压缩 true or false
                .isGif(true)// 是否显示gif图片 true or false
                .openClickSound(true)// 是否开启点击声音 true or false
                .previewEggs(true)// 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中) true or false
                .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){

        }
        return super.onKeyDown(keyCode, event);
    }
}
